package com.my.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.my.bean.User;
import com.my.dao.IBaseDAO;
import com.my.dao.UserDAO;

public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

	private IBaseDAO<User> basedao;

	public void setBasedao(IBaseDAO<User> basedao) {
		this.basedao = basedao;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int checkLogin(User user) {
		Session session = this.getSession();
		String hql = "from User as u where u.userName=? and u.passWord=?";
		Query query = session.createQuery(hql);

		query.setString(0, user.getUserName());
		query.setString(1, user.getPassWord());
		List<User> list = query.list();
		if (list != null && list.size() > 0) {
			return list.get(0).getId();
		}
		return 0;
	}

	@Override
	public User checkUser(User user) {
		String hql = "from User as u where u.userName=? and u.passWord=? and u.role.id = ?";
		String name = user.getUserName();
		String pass = user.getPassWord();
		String roleId = user.getRole().getId().toString();
		return basedao.checkEntity(hql, name, pass, roleId);
	}

}
